//
//  RMMCommonBut.m
//  Romomo
//
//  Created by puman on 2017/2/9.
//  Copyright © 2017年 mazegong. All rights reserved.
//

#import "RMMCommonBut.h"

@implementation RMMCommonBut
- (void)dealloc {
    [_nameLabel removeObserver:self forKeyPath:@"text" context:nil];
}
- (instancetype)initWithFrame:(CGRect)frame title:(NSString *)title {
    if ([super initWithFrame:frame]) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.text = title;
        _nameLabel.textColor = kWhiteColor;
        _nameLabel.font = [UIFont systemFontOfSize:16];
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_nameLabel];
        
        _titleImageView = [[UIImageView alloc] init];
        _titleImageView.image = [UIImage imageNamed:@"more-21@2x"];
        [self addSubview:_titleImageView];
        
        [_nameLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
        [self setUpWithTitle:title];
        self.selected = NO;
    }
    return self;
}
- (void)setUpWithTitle:(NSString *)title {
    CGSize size = [title sizeWithAttributes: @{NSFontAttributeName:[UIFont systemFontOfSize:16]}];
    CGFloat width = (self.frame.size.width - size.width- 20)/2;
    _nameLabel.frame = CGRectMake(width, 0, size.width, 30);
    _titleImageView.frame = CGRectMake(width+size.width+3, 7, 16, 16);
}
- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context {
    if ([keyPath isEqualToString:@"text"]) {
        NSString *name = [change valueForKey:NSKeyValueChangeNewKey];
        [self setUpWithTitle:name];
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
